import tkinter
import tkmacosx
import pymysql



def find_login():
    db = pymysql.connect(host='180.102.129.53', user='root', passwd='520hhy', db='hhy', charset='utf8')
    cursor = db.cursor()
    sql = """select * from admin"""
    cursor.execute(sql)
    res = cursor.fetchall()
    return res

def find_user():
    db = pymysql.connect(host='180.102.129.53', user='root', passwd='520hhy', db='hhy', charset='utf8')
    cursor = db.cursor()
    sql = """select * from login"""
    cursor.execute(sql)
    res = cursor.fetchall()
    return res

def find_liuyan():
    db = pymysql.connect(host='180.102.129.53', user='root', passwd='520hhy', db='hhy', charset='utf8')
    cursor = db.cursor()
    sql = """select * from message"""
    cursor.execute(sql)
    res = cursor.fetchall()
    return res


class ShowAlg:
    def __init__(self, alg_name):
        global root
        self.alg_name = alg_name
        self.frame = tkmacosx.SFrame(root,width=800, height=500)
        self.name = tkinter.Label(self.frame,text="算法可视化管理员维护平台", fg='black', font=("微软雅黑", 20, 'bold'))
        self.bt1 = tkmacosx.Button(self.frame, text="用户管理", width=100, height=50, command=lambda :self.change_area_text(1))
        self.bt2 = tkmacosx.Button(self.frame, text="登陆管理", width=100, height=50, command=lambda :self.change_area_text(2))
        self.bt3 = tkmacosx.Button(self.frame, text="留言管理", width=100, height=50, command=lambda :self.change_area_text(3))
        self.area = tkinter.Text(self.frame, height=30, width=55)
        self.area.config(state="disable")

    def change_area_text(self, what):
        self.area.config(state="normal")
        content = ""
        if what==2:
            res = find_login()
            for i in res:
                sig = i[1]+"在"+i[2]+"登陆了系统"
                content = content+sig+"\n"
        elif what == 1:
            res = find_user()
            content = ""
            for i in res:
                print(i)
                sig = "用户："+i[0]+"密码为："+i[1]
                content = content+sig+"\n"
        elif what == 3:
            res = find_liuyan()
            for i in res:
                sig = "用户："+i[1]+"在"+i[5]+"留言中说："+i[4]
                content = content + sig
        self.area.delete('1.0', 'end')
        self.area.insert("end", content)
        self.area.config(state="disable")

    def grid(self):
        self.name.grid(row=0, column=0, columnspan=2)
        self.bt1.grid(row=1, column=1)
        self.bt2.grid(row=2, column=1)
        self.bt3.grid(row=3, column=1)
        self.area.grid(row=1, column=2, rowspan=3)
        self.frame.grid(row=0,column=0)


def create_one():
    global root
    root = tkinter.Toplevel()
    root.title("算法可视化")
    root.geometry("800x500")
    c1 = ShowAlg("")
    c1.grid()
    c1.change_area_text("info")
    root.mainloop()
